package com.zhaoyue.study.io.bio;

import com.sun.deploy.util.StringUtils;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 2017/1/3
 * Time: 15:00
 * To change this template use File | Settings | File Templates.
 */
public class TimeServerHandler implements Runnable {

    private Socket socket = null;

    public TimeServerHandler(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName() + "线程开始处理请求");
        BufferedReader in = null;
        PrintWriter out = null;
        try {
            in = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
            out = new PrintWriter(this.socket.getOutputStream(),true);
            String body = null;
            while (true) {
                body = in.readLine();
                System.out.println("aa:"+body);
                if (body == null) {
                    break;
                }
                System.out.println("接收的命令：" + body);
                out.println("当前时间：" + System.currentTimeMillis());
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (socket != null) {
                    socket.close();
                    socket = null;
                }
                if (in != null) {
                    in.close();
                }
                if (out != null) {
                    out.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
